package com.example.service.impl;

import com.example.dao.UserMapper;
import com.example.entity.*;
import com.example.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl implements UserService {
    private final UserMapper mapper;
    private final SqlSession sqlSession;

    public UserServiceImpl(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
        this.mapper = sqlSession.getMapper(UserMapper.class);
    }

    @Override
    public void commit() {
        sqlSession.commit();
    }

    @Override
    public void rollback() {
        sqlSession.rollback();
    }

    //登录
    @Override
    public boolean auth(String u_account, String u_password, HttpSession session) {
        User user = mapper.getUser(u_account, u_password);
        if (user == null) return false;
        session.setAttribute("user", user);
        return true;
    }
    @Override
    public User getUser(String account, String password) {
        return mapper.getUser(account, password);
    }
    @Override
    public String getHashedPasswordByAccount(String account) {
        return mapper.getHashedPasswordByAccount(account);
    }

    @Override
    public User getUserByAccount(String u_account) {
        return mapper.getUserByAccount(u_account);
    }

    @Override
    public Passenger getPassengerByIDtypeAndIDcard(String ID_type, String ID_card) {
        return mapper.getPassengerByIDtypeAndIDcard(ID_type, ID_card);
    }

    @Override
    public int addUser(String name, String sex, String ID_type, String ID_card, String account, String password, String phone, String birthday, String create_date) {
        return mapper.addUser(name, sex, ID_type, ID_card, account, password, phone, birthday, create_date);
    }
    @Override
    public int getUidByIDcard(String ID_card) {
        return mapper.getUidByIDcard(ID_card);
    }
    @Override
    public Integer getPidByUid(int u_id) {
        Integer pid = mapper.getPidByUid(u_id);
        return pid != null ? pid : 0;
    }
    //    @Override
//    public int getOrderNumByPid(int p_id) {
//        return mapper.getOrderNumByPid(p_id);
//    }
    @Override
    public int addBank(String u_name,String ID_type, String u_ID_card) { return mapper.addBank(u_name, ID_type, u_ID_card); }

    //注销
    @Override
    public int deleteUser(int u_id) {
        return mapper.deleteUser(u_id);
    }
    @Override
    public List<Ticket> getTicketListByOrderId(int o_id) {
        return mapper.getTicketListByOrderId(o_id);
    }
    @Override
    public int addEconomySpace(int f_id) {
        return mapper.addEconomySpace(f_id);
    }
    @Override
    public int addBusinessSpace(int f_id) {
        return mapper.addBusinessSpace(f_id);
    }
    @Override
    public int addFirstSpace(int f_id) {
        return mapper.addFirstSpace(f_id);
    }
    @Override
    public int deleteTicket(int t_id) {
        return mapper.deleteTicket(t_id);
    }

    @Override
    public int deleteOrderById(int o_id) {
        return mapper.deleteOrderById(o_id);
    }

    @Override
    public int deleteOrder(int u_id) {
        return mapper.deleteOrder(u_id);
    }
    @Override
    public int addBankMoneyByUserIDtypeAndIDcard(String ID_type, String ID_card, double money) {
        return mapper.addBankMoneyByUserIDtypeAndIDcard(ID_type, ID_card, money);
    }
    @Override
    public List<Ticket> getTicketListByPassengerId(int passenger_id) {
        return mapper.getTicketListByPassengerId(passenger_id);
    }
    @Override
    public Order getOrderById(int o_id) {
        return mapper.getOrderById(o_id);
    }
    @Override
    public User getUserById(int u_id) {
        return mapper.getUserById(u_id);
    }
    @Override
    public int updateOrderPriceAndPassengerNum(int o_id, double price) {
        return mapper.updateOrderPriceAndPassengerNum(o_id, price);
    }
    @Override
    public int deletePassenger(int passenger_id) {
        return mapper.deletePassenger(passenger_id);
    }

    @Override
    public int isExistByAccount(String account) {
        return mapper.isExistByAccount(account);
    }

    @Override
    public int isExistByIDcardAndIDtype(String ID_type, String ID_card) {
        return mapper.isExistByIDtypeAndIDcard(ID_type, ID_card);
    }
    @Override
    public int isExistByPhone(String phone) {
        return mapper.isExistByPhone(phone);
    }
    @Override
    public int isExistByEmail(String email) {
        return mapper.isExistByEmail(email);
    }
    @Override
    public int updateUser(String name, String sex, String ID_card, String account, String password, String phone,String city) {
        return mapper.updatePassenger(name, sex, ID_card, account, password, phone,city);
    }
    //修改个人信息

    @Override
    public int updatePassword(String password, String account) {
        return mapper.updatePassword(password, account);
    }

    //订单
    @Override
    public List<Order> getOrderListByUserId(int userId) {
        return mapper.getOrderListByUserId(userId);
    }
    @Override
    public int getOrderNumByUserId(int userId) {
        return mapper.getOrderNumByUserId(userId);
    }
    @Override
    public int getUnpaidOrderNum(int userId) {
        return mapper.getUnpaidOrderNum(userId);
    }
    @Override
    public int getPaidOrderNum(int userId) {
        return mapper.getPaidOrderNum(userId);
    }
    @Override
    public int getCompletedOrderNum(int userId) {
        return mapper.getCompletedOrderNum(userId);
    }
    @Override
    public int getCancelledOrderNum(int userId) {
        return mapper.getCancelledOrderNum(userId);
    }
    @Override
    public int getRefundOrderNum(int userId) {
        return mapper.getRefundOrderNum(userId);
    }

    //机票
    @Override
    public List<TicketDetail> getTicketDetailsListByPassengerId(int passenger_id) {
        return mapper.getTicketDetailsListByPassengerId(passenger_id);
    }
    @Override
    public int getTicketNumByPassengerId(int passenger_id) {
        return mapper.getTicketNumByPassengerId(passenger_id);
    }
    @Override
    public int getUpcomingTicketNumByPassengerId(int passenger_id) {
        return mapper.getUpcomingTicketNumByPassengerId(passenger_id);
    }
    @Override
    public int getCompletedTicketNumByPassengerId(int passenger_id) {
        return mapper.getCompletedTicketNumByPassengerId(passenger_id);
    }
    @Override
    public int getCancelledTicketNumByPassengerId(int passenger_id) {
        return mapper.getCancelledTicketNumByPassengerId(passenger_id);
    }
    @Override
    public int getPlannedTicketNumByPassengerId(int passenger_id) {
        return mapper.getPlannedTicketNumByPassengerId(passenger_id);
    }
    @Override
    public int getUnpaidTicketNumByPassengerId(int passenger_id) {
        return mapper.getUnpaidTicketNumByPassengerId(passenger_id);
    }
    @Override
    public int getRefundTicketNumByPassengerId(int passenger_id) {
        return mapper.getRefundTicketNumByPassengerId(passenger_id);
    }
    @Override
    public int getDelayTicketNumByPassengerId(int passenger_id) {
        return mapper.getDelayTicketNumByPassengerId(passenger_id);
    }
    //预定机票
    @Override
    public Flight getFlightById(int f_id) {
        return mapper.getFlightById(f_id);
    }
    @Override
    public int isTicketExist(int p_id, int f_id) {
        return mapper.isTicketExist(p_id, f_id);
    }
    @Override
    public FlightBooking getFlightBookingByFid(int f_id) {
        return mapper.getFlightBookingByFid(f_id);
    }
    @Override
    public FlightStatus getFlightStatusById(int f_id) {
        return mapper.getFlightStatusById(f_id);
    }
    @Override
    public int getLastOrderId() {
        return mapper.getLastOrderId();
    }
    @Override
    public double getEconomyPrice(int f_id) {
        return mapper.getEconomyPriceByFid(f_id);
    }
    @Override
    public double getEconomyChildPriceByFid(int f_id) {
        return mapper.getEconomyChildPriceByFid(f_id);
    }
    @Override
    public double getEconomySeniorPriceByFid(int f_id) {
        return mapper.getEconomySeniorPriceByFid(f_id);
    }
    @Override
    public double getEconomyInfantPriceByFid(int f_id) {
        return mapper.getEconomyInfantPriceByFid(f_id);
    }
    @Override
    public double getBusinessPriceByFid(int f_id) {
        return mapper.getBusinessPriceByFid(f_id);
    }
    @Override
    public double getBusinessChildPriceByFid(int f_id) {
        return mapper.getBusinessChildPriceByFid(f_id);
    }
    @Override
    public double getBusinessSeniorPriceByFid(int f_id) {
        return mapper.getBusinessSeniorPriceByFid(f_id);
    }
    @Override
    public double getBusinessInfantPriceByFid(int f_id) {
        return mapper.getBusinessInfantPriceByFid(f_id);
    }
    @Override
    public double getFirstPriceByFid(int f_id) {
        return mapper.getFirstPriceByFid(f_id);
    }
    @Override
    public double getFirstChildPriceByFid(int f_id) {
        return mapper.getFirstChildPriceByFid(f_id);
    }
    @Override
    public double getFirstSeniorPriceByFid(int f_id) {
        return mapper.getFirstSeniorPriceByFid(f_id);
    }
    @Override
    public double getFirstInfantPriceByFid(int f_id) {
        return mapper.getFirstInfantPriceByFid(f_id);
    }
    @Override
    public double getBankMoneyByUserIDtypeAndIDcard(String ID_type, String ID_card) {
        return mapper.getBankMoneyByUserIDtypeAndIDcard(ID_type, ID_card);
    }
    @Override
    public int updateBankMoneyByUserIDtypeAndIDcard(String ID_type, String ID_card, double money) {
        return mapper.updateBankMoneyByUserIDtypeAndIDcard(ID_type, ID_card, money);
    }
    @Override
    public int isPassengerExistByIDtypeAndIDcard(String ID_type, String ID_card) {
        return mapper.isPassengerExistByIDtypeAndIDcard(ID_type, ID_card);
    }
    @Override
    public int updatePassengerOrderNum(String ID_type, String ID_card) {
        return mapper.updatePassengerOrderNum(ID_type, ID_card);
    }
    @Override
    public int addPassenger(String name, String sex, String ID_type, String ID_card, String birthday) {
        return mapper.addPassenger(name, sex, ID_type, ID_card, birthday);
    }
    @Override
    public int updateEconomySpace(int f_id) {
        return mapper.updateEconomySpace(f_id);
    }
    @Override
    public int updateBusinessSpace(int f_id) {
        return mapper.updateBusinessSpace(f_id);
    }
    @Override
    public int updateFirstSpace(int f_id) {
        return mapper.updateFirstSpace(f_id);
    }
    @Override
    public int getPassengerIdByIDtypeAndIDcard(String ID_type, String ID_card) {
        return mapper.getPassengerIdByIDtypeAndIDcard(ID_type, ID_card);
    }
    @Override
    public int addTicket(int order_id, int passenger_id, String passenger_name, String passenger_type, int flightNo, String date, double price, String seat_type, String status, String statusText) {
        return mapper.addTicket(order_id, passenger_id, passenger_name, passenger_type, flightNo, date, price, seat_type, status, statusText);
    }
    @Override
    public int addOrder(int o_id,int userId, String user_name, int flightNo, String airline, String date, String from, String to, String departure, String arrival, double price, int passengers, String status, String statusText, String paymentMethod, String paymentDate) {
        return mapper.addOrder(o_id,userId, user_name, flightNo, airline, date, from, to, departure, arrival, price, passengers, status, statusText, paymentMethod, paymentDate);
    }

    //机票操作
//    @Override
//    public Ticket getTicketById(int ticketId) {
//        return mapper.getTicketById(ticketId);
//    }
//    @Override
//    public int cancelTicket(int ticketId) {
//        return mapper.cancelTicket(ticketId);
//    }
//    @Override
//    public int getPassengersByOrderId(int orderId) {
//        return mapper.getPassengersByOrderId(orderId);
//    }
//    @Override
//    public int cancelOrder(int orderId, double price, String cancelDate) {
//        return mapper.cancelOrder(orderId, price, cancelDate);
//    }
//    @Override
//    public int addEconomySpaceByFid(int f_id) {
//        return mapper.addEconomySpaceByFid(f_id);
//    }
//    @Override
//    public int addBusinessSpaceByFid(int f_id) {
//        return mapper.addBusinessSpaceByFid(f_id);
//    }
//    @Override
//    public int addFirstSpaceByFid(int f_id) {
//        return mapper.addFirstSpaceByFid(f_id);
//    }
//    @Override
//    public int reducePassengerOrderNumByPassengerId(int passenger_id) {
//        return mapper.reducePassengerOrderNumByPassengerId(passenger_id);
//    }
//    @Override
//    public int refundMoneyByUserIDtypeAndIDcard(String ID_type, String u_ID_card, double money) {
//        return mapper.refundMoneyByUserIDtypeAndIDcard(ID_type, u_ID_card, money);
//    }
//    @Override
//    public int changeTicket(int ticketId, int flightNo, double price) {
//        return mapper.changeTicket(ticketId, flightNo, price);
//    }
//    @Override
//    public int reduceEconomySpaceByFid(int f_id) {
//        return mapper.reduceEconomySpaceByFid(f_id);
//    }
//    @Override
//    public int reduceBusinessSpaceByFid(int f_id) {
//        return mapper.reduceBusinessSpaceByFid(f_id);
//    }
//    @Override
//    public int reduceFirstSpaceByFid(int f_id) {
//        return mapper.reduceFirstSpaceByFid(f_id);
//    }
//    @Override
//    public int changeMoneyByUserIDtypeAndIDcard(String ID_type, String u_ID_card, double money) {
//        return mapper.changeMoneyByUserIDtypeAndIDcard(ID_type, u_ID_card, money);
//    }
//
//    //订单操作
//    @Override
//    public Order getOrderById(int orderId) {
//        return mapper.getOrderById(orderId);
//    }
//    @Override
//    public int addEconomySpaceByFid(int passengers, int f_id) {
//        return mapper.addEconomySpaceByFid(passengers, f_id);
//    }
//    @Override
//    public int addBusinessSpaceByFid(int passengers, int f_id) {
//        return mapper.addBusinessSpaceByFid(passengers, f_id);
//    }
//    @Override
//    public int addFirstSpaceByFid(int passengers, int f_id) {
//        return mapper.addFirstSpaceByFid(passengers, f_id);
//    }
//    @Override
//    public int cancelOrder(int orderId, String cancelDate) {
//        return mapper.cancelOrder(orderId, cancelDate);
//    }
//    @Override
//    public int cancelTicketByOrderId(int orderId) {
//        return mapper.cancelTicketByOrderId(orderId);
//    }
//    @Override
//    public int payMoneyByUserIDtypeAndIDcard(String ID_type, String u_ID_card, double money) {
//        return mapper.payMoneyByUserIDtypeAndIDcard(ID_type, u_ID_card, money);
//    }
//    @Override
//    public int payOrder(int orderId) {
//        return mapper.payOrder(orderId);
//    }
//    @Override
//    public int addPassengerOrderNumByPassengerId(int passenger_id) {
//        return mapper.addPassengerOrderNumByPassengerId(passenger_id);
//    }
//    @Override
//    public int scheduledTicketStatus(int ticketId) {
//        return mapper.scheduledTicketStatus(ticketId);
//    }
//    @Override
//    public int reduceEconomySpaceByFid(int passengers, int f_id) {
//        return mapper.reduceEconomySpaceByFid(passengers, f_id);
//    }
//    @Override
//    public int reduceBusinessSpaceByFid(int passengers, int f_id) {
//        return mapper.reduceBusinessSpaceByFid(passengers, f_id);
//    }
//    @Override
//    public int reduceFirstSpaceByFid(int passengers, int f_id) {
//        return mapper.reduceFirstSpaceByFid(passengers, f_id);
//    }
//    @Override
//    public int changeTicketStatus(int flightNo, String airline, int orderId, double price, String paymentMethod, String paymentDate) {
//        return mapper.changeTicketStatus(flightNo, airline, orderId, price, paymentMethod, paymentDate);
//    }
//    @Override
//    public int changeTicketByOrderId(int orderId, int flightNo, double price) {
//        return mapper.changeTicketByOrderId(orderId, flightNo, price);
//    }

    //绑定邮箱
    @Override
    public String getEmailByUserId(int u_id) {
        return mapper.getEmailByUserId(u_id);
    }
    @Override
    public int updateEmail(int u_id, String email) {
        return mapper.updateEmailByUserId(u_id, email);
    }
}
